<?php

 
Zend_Loader::loadClass("BaseDBRows");
Zend_Loader::loadClass("CalendarYearMonthName");
class CalendarYearMonthNames extends BaseDBRows
{
	protected function setUp()
	{
		$this->_tableName = CalendarYearMonthName::_tablename;
	}	
	
	public function getReceiptSupplierByOrgId($orgId)
	{
		Zend_Loader::loadClass("Organization");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("r.supplier_org_id, org.name supplier_name");
		$query->addFrom($this->_tableName . " r ");
		$query->addJoin("," . Organization::_tablename . " org ");
		$query->addWhere(" r.supplier_org_id = org.org_id ");
		$query->addWhere(" r.org_id = " . $orgId);
		$query->addGroupBy("r.supplier_org_id, org.name");
		
		$db = DBCon::instance();
		// echo  $query->createSQL();die();
		$result = $db->executeQuery($query->createSQL());
		return $result;
	}
	
	public function getYearMonthNames($dateFrom="",$dateTo="") {
	
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect(" * ");
		$query->addFrom($this->_tableName);
		$query->addOrderBy("`year_month`");
		
		// dates from - to
		if(strlen($dateFrom) > 0 && strlen($dateTo) > 0) {
			$query->addWhere(" `year_month` between date_format(least('" . $dateFrom . "','" . $dateTo . "'),'%Y%m') and date_format(greatest('" . $dateFrom . "','" . $dateTo . "'),'%Y%m')" );	
		}
		elseif(strlen($dateFrom) > 0) {
			$query->addWhere(" `year_month` >= date_format('" . $dateFrom . "','%Y%m')" );
		}
		elseif(strlen($dateTo) > 0) {
			$query->addWhere(" `year_month` <= date_format('" . $dateTo . "','%Y%m')");
		}
		else {
			$query->addWhere(" `year_month` between date_format(date_format(curdate(),'%Y-01-01'),'%Y%m') and date_format(date_format(curdate(),'%Y-12-31'),'%Y%m')");
		}
		
		$db = DBCon::instance();
		// echo  $query->createSQL();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}

}
?>